<template>
  <div>

    <el-card class="box-card">

      <el-dialog title="秒杀商品" :visible.sync="dialogFormVisible">

        <el-form :model="goodsForm" :rules="userRules" ref="userForm" label-width="80px">
          <!--            <el-row>-->
          <!--              <el-col :span="12">-->
          <!--            <el-form-item label="头像">-->
          <!--              <el-upload-->
          <!--                class="avatar-uploader"-->
          <!--                action="/api/user/upload"-->
          <!--                :headers="auth"-->
          <!--                :show-file-list="false"-->
          <!--                :on-success="handleAvatarSuccess"-->
          <!--                :before-upload="beforeAvatarUpload">-->
          <!--                <img v-if="goodsForm.image!=null" :src="'/api/goods/download/'+goodsForm.image" class="avatar">-->
          <!--                <i v-else class="el-icon-plus avatar-uploader-icon"></i>-->
          <!--              </el-upload>-->
          <!--            </el-form-item>-->
          <!--              </el-col>-->
          <!--            </el-row>-->
          <el-row>
            <el-col :span="12">
            <el-form-item label="上传图片" prop="pic">
              <el-upload
                class="avatar-uploader"
                action="/api/goods/upload"
                :show-file-list="false"
                :on-success="handleAvatarSuccess"
                :before-upload="beforeAvatarUpload">
                <img v-if="this.goodsForm.pic!=null" :src="'api/goods/download/'+this.goodsForm.pic" class="avatar">
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
            </el-form-item>
            </el-col>
          </el-row>
          <el-row >
            <el-col :span="20">
              <el-form-item label="商品标题" prop="goodsTitle">
                <el-input v-model="goodsForm.goodsTitle" autocomplete="off"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
            <el-row :gutter="20">
              <el-col :span="10">
                <el-form-item label="商品名称" prop="goodsName">
                  <el-input v-model="goodsForm.goodsName" autocomplete="off"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="10">
                <el-form-item label="商品类型" >
                  <el-select v-model="goodsForm.type" placeholder="请选择">
                    <el-option label="手机" value="手机"></el-option>
                    <el-option label="电脑" value="电脑"></el-option>
                    <el-option label="服装" value="服装"></el-option>
                    <el-option label="化妆品" value="化妆品"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="20">
            <el-col :span="10">
              <el-form-item label="原价格" prop="price">
                <el-input v-model="goodsForm.price" autocomplete="off"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="10">
              <el-form-item label="秒杀价格" prop="costPrice">
                <el-input v-model="goodsForm.costPrice" autocomplete="off"></el-input>
              </el-form-item>
            </el-col>
            </el-row>
            <el-row :gutter="20">
            <el-col :span="10">
              <el-form-item label="秒杀数量" prop="num">
                <el-input v-model="goodsForm.num" autocomplete="off"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="10">
              <el-form-item label="库存量" prop="stockNum">
                <el-input v-model="goodsForm.stockNum=goodsForm.num" :disabled="true" autocomplete="off"></el-input>
              </el-form-item>
            </el-col>
            </el-row>
            <el-row :gutter="20">
            <el-col :span="10">
              <el-form-item label="开始时间" prop="startTime">
                <el-date-picker
                  v-model="goodsForm.startTime"
                  type="datetime"
                  placeholder="选择日期时间"
                  default-time="10:00:00">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="10">
              <el-form-item label="结束时间" prop="endTime" :disabled="true">
                <el-date-picker
                  v-model="goodsForm.endTime"
                  type="datetime"
                  placeholder="选择日期时间"
                  default-time="10:00:00">
                </el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="save">确 定</el-button>

      </el-dialog>







      <el-form ref="form" :model="sellorForm" label-width="80px">
        <el-form-item label="商品名称">
          <el-input v-model="sellorForm.goodsName" style="width: 200px"></el-input>
          <el-button type="primary" @click="searchList">搜索</el-button>
          <el-button type="primary" @click="addSave">添加秒杀商品</el-button>
        </el-form-item>
      </el-form>



      <el-table
        :data="tableData"
        style="width: 100%">
        <!--        <el-table-column-->
        <!--          label="图片">-->
        <!--          <template slot-scope="scope">-->
        <!--            <img  v-if="scope.row.image!=null" :src="'api/user/download/'+scope.row.image" width="50px" height="50px">-->
        <!--          </template>-->
        <!--        </el-table-column>-->
        <el-table-column
          prop="goodsName"
          label="商品名称"
          width="100">
        </el-table-column>
        <el-table-column
          label="缩略图">
          <template slot-scope="scope">
            <img  v-if="scope.row.pic!=null" :src="'api/goods/download/'+scope.row.pic" width="50px" height="50px">
          </template>
        </el-table-column>
        <el-table-column
          prop="goodsTitle"
          label="商品标题"
          width="180">
        </el-table-column>
        <el-table-column
          prop="type"
          label="商品类型"
          width="180">
        </el-table-column>
        <el-table-column
          prop="price"
          label="原价格">
        </el-table-column>
        <el-table-column
          prop="costPrice"
          label="秒杀价格">
        </el-table-column>
        <el-table-column
          label="秒杀开始时间" width="180px">
          <template slot-scope="scope">
            {{scope.row.startTime| dateFit('YYYY-MM-DD HH:mm:ss')}}
          </template>
        </el-table-column>
        <el-table-column
          label="秒杀结束时间" width="180px">
          <template slot-scope="scope">
            {{scope.row.endTime| dateFit('YYYY-MM-DD HH:mm:ss')}}
          </template>
        </el-table-column>
        <el-table-column
          prop="num"
          label="秒杀数量">
        </el-table-column>
        <el-table-column
          prop="stockNum"
          label="库存数量">
        </el-table-column>
        <el-table-column
          label="状态">
          <template slot-scope="scope">
            {{scope.row.status|stateFit}}
          </template>
        </el-table-column>
        <el-table-column
          fixed="right"
          label="操作"
          width="100">
          <template slot-scope="scope">
            <el-button @click="updateGoods1(scope.row.id)" v-if="scope.row.status==0" type="text" size="small">发布</el-button>
            <el-button @click="updateGoods2(scope.row.id)" type="text" v-if="scope.row.status==1" size="small">上架</el-button>
            <el-button @click="updateGoods3(scope.row.id)" type="text" v-if="scope.row.status==2" size="small">下架</el-button>
            <el-button @click="delGoods(scope.row.id)" type="text" v-if="scope.row.status==1" size="small">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5, 10, 20, 30]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>

    </el-card>

  </div>
</template>

<script>
    export default {
        name: "GoodsList",
      data(){
        return{
          tableData:[],
          sellorForm:{},
          currentPage:0,
          pageSize:5,
          total:0,
          dialogFormVisible:false,
          goodsForm:{pic:''},
          pic:'',
          userRules:{
            goodsTitle:[{required: true, message: '请输入用户名', trigger: 'blur'}],
            goodsName:[{required: true, message: '请输入用户名', trigger: 'blur'}],
            price:[
              {pattern: /\d/, message: '请输入数字', trigger: 'blur'},
              {required: true, message: '请输入单价', trigger: 'blur'}
            ],
            costPrice:[
              {pattern: /\d/, message: '请输入数字', trigger: 'blur'},
              {required: true, message: '请输入单价', trigger: 'blur'}
            ],
            num:[{number: true, message: '请输入数字', trigger: 'blur'},]
          }
        }
      },
      methods:{
        searchList:function () {
          var sid=JSON.parse(sessionStorage.getItem("user")).id;
          this.$http.post("/goods/list/"+this.currentPage+"/"+this.pageSize+"/"+sid,this.sellorForm).then((res)=>{
            this.tableData=res.data.result.list;
            this.total=res.data.result.total;
          })
        },
        handleSizeChange(val) {
          this.pageSize=val;
          this.searchList();
        },
        handleCurrentChange(val) {
          this.currentPage=val;
          this.searchList();
        },
        //添加页面
        addSave:function () {
          this.dialogFormVisible=true;
        },
        save:function () {
          this.$refs['userForm'].validate((valid)=>{
            if(valid){
              this.goodsForm.sellerId=JSON.parse(sessionStorage.getItem("user")).id
              this.$http.post("/goods/addGoods",this.goodsForm).then((res)=>{
                if(res.data.success){
                  alert(res.data.message);
                  this.dialogFormVisible=false;
                }else{
                  alert(res.data.message);
                }
              })
            }
          })
        },
        updateGoods1:function (id) {
          this.$http.post("/goods/updateGoods1/"+id).then((res)=>{
            if(res.data.success){
              alert(res.data.message);
              this.searchList();
            }else{
              alert(res.data.message);
            }
          })
        },
        updateGoods2:function (id) {
          this.$http.post("/goods/updateGoods2/"+id).then((res)=>{
            if(res.data.success){
              alert(res.data.message);
              this.searchList();
            }else{
              alert(res.data.message);
            }
          })
        },
        updateGoods3:function (id) {
          this.$http.post("/goods/updateGoods3/"+id).then((res)=>{
            if(res.data.success){
              alert(res.data.message);
              this.searchList();
            }else{
              alert(res.data.message);
            }
          })
        },
        delGoods:function (id) {
          this.$http.post("/goods/delGoods/"+id).then((res)=>{
            if(res.data.success){
              alert(res.data.message);
              this.searchList();
            }else{
              alert(res.data.message);
            }
          })
        },
        handleAvatarSuccess(res, file) {
          this.goodsForm.pic=res.result.path;
        },
        beforeAvatarUpload(file) {
          const isJPG = file.type === 'image/jpeg'||file.type === 'image/png';
          const isLt2M = file.size / 1024 / 1024 < 2;

          if (!isJPG) {
            this.$message.error('上传头像图片只能是 JPG 格式!');
          }
          if (!isLt2M) {
            this.$message.error('上传头像图片大小不能超过 2MB!');
          }
          return isJPG && isLt2M;
        },
      },
      mounted() {
        this.searchList();
      },
      filters:{
        stateFit:function (val) {
          if(val==0){
            return '未发布';
          }
          if(val==1){
            return '下架';
          }
          if(val==2){
            return '上架';
          }
        }
      }
    }
</script>

<style scoped>

</style>
